package com.web.controller;

import com.alibaba.fastjson2.JSON;
import com.web.entity.Course;
import com.web.entity.ResponseResult;
import com.web.entity.ResponseResult;
import com.web.service.CourseService;
import com.web.service.impl.CourseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Songzzz
 * @Description
 * @Date 2025/3/6 17:30
 */
@WebServlet("/findCourse")
public class FindCourseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求和响应编码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        // 设置响应类型
        resp.setContentType("application/json;charset=UTF-8");

        CourseService courseService = new CourseServiceImpl();
        List<Course> courses = courseService.findAllCourse();

        // 添加日志，打印查询结果
        System.out.println("Courses: " + courses);

        // 返回 JSON 响应
        resp.getWriter().write(JSON.toJSONString(new ResponseResult(200, "OK", courses)));
    }
}
